package org.codeart.map.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.codeart.common.constant.MapResultKey;
import org.codeart.common.dto.DicDistrict;
import org.codeart.map.mapper.DicDistrictMapper;
import org.codeart.map.remote.MapDicDistrictClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class DicDistrictService {

    @Autowired
    private MapDicDistrictClient mapDicDistrictClient;

    @Autowired
    private DicDistrictMapper dicDistrictMapper;

    public boolean initDicDistrict(String keywords) {
        String dicDistrict = mapDicDistrictClient.dicDistrict(keywords);
        try {
            JSONObject jsonObject = JSONObject.parseObject(dicDistrict);
            Optional.ofNullable(jsonObject)
                    .map(obj -> obj.getInteger(MapResultKey.STATUS))
                    .filter(status -> status == 1)
                    .map(obj -> jsonObject.getJSONArray(MapResultKey.DISTRICTS))
                    .ifPresent(jsonArray -> {
                        List<DicDistrict> list = new ArrayList<>();
                        treeToList(jsonArray, MapResultKey.DISTRICTS, null, list);
                        dicDistrictMapper.insertBatch(list);
                    });
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 把JSON树形数据转为列表型
     * @param array     json数组
     * @param pid       当前父节点的id
     * @param field     子节点字段
     * @param collector 收集器
     */
    public void treeToList(JSONArray array, String field, String pid, List<DicDistrict> collector) {
        if (array != null) {
            for (int i = 0; i < array.size(); i++) {
                JSONObject jsonObject = array.getJSONObject(i);

                String level = jsonObject.getString(MapResultKey.LEVEL);
                if (level.equals(MapResultKey.LEVEL)) {
                    return;
                }
                String addressCode = jsonObject.getString(MapResultKey.AD_CODE);
                String name = jsonObject.getString(MapResultKey.NAME);
                
                DicDistrict district = new DicDistrict();
                district.setAddressCode(addressCode)
                        .setAddressName(name)
                        .setLevel(convertLevel(level))
                        .setParentAddressCode(pid);
                collector.add(district);

                // 递归调用
                JSONArray arr = jsonObject.getJSONArray(field);
                treeToList(arr, field, addressCode, collector);
            }
        }
    }

    private int convertLevel(String level) {
        int levelInt = 0;
        switch (level.trim()) {
            case "country":
                levelInt = 0;
                break;
            case "province":
                levelInt = 1;
                break;
            case "city":
                levelInt = 2;
                break;
            case "district":
                levelInt = 3;
                break;
            case "street":
                levelInt = 4;
                break;
        }
        return levelInt;
    }
}
